package com.zjs.timing;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.*;

/**
 * 定时任务配置文件
 *
 * @author zhu_jinsheng
 * @version 1.0
 * @date 2020/7/6 15:32
 */
@Configuration
public class ScheduleConfig  implements SchedulingConfigurer {

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        /**
         * nameFormat- 一个String.format(String, Object...)兼容的格式字符串,将作为单个参数提供唯一的整数（0,1等）
         * 此整数对于ThreadFactory的构建实例是唯一的，并将按顺序分配。
         * 例如，"rpc-pool-%d"会产生像线程名称 "rpc-pool-0"，"rpc-pool-1"，"rpc-pool-2"
         */
        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("schedule-thread-%d").build();
        ScheduledThreadPoolExecutor poolExecutor = new ScheduledThreadPoolExecutor(
                10,
                threadFactory,
                new ThreadPoolExecutor.AbortPolicy());
        taskRegistrar.setScheduler(poolExecutor);
    }

    public void startScheduledTask() {

    }
}
